package com.fanfanwang.ffw.app.ui.service;


import java.util.ArrayList;

import com.android.volley.toolbox.NetworkImageView;
import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.asynctask.GetNewsInfoAsyncTask;
import com.fanfanwang.ffw.app.base.BaseActivity;
import com.fanfanwang.ffw.app.bean.NewsInfoBean;
import com.fanfanwang.ffw.app.bean.NewsListBean;
import com.fanfanwang.ffw.app.tools.TimeUtils;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 新闻资讯详情
 * 
 * @author Administrator
 * 
 */
public class ServiceNewListDetailAct extends BaseActivity {
	private ServiceNewListDetailAct mAct;
	private CommonTitleBar commonTitleBar;
	//	private WebView webView;

	/**
	 * 网络错误布局layout
	 */
	private LinearLayout llNetError;
	/**
	 * 网络错误显示图片
	 */
	private ImageView ivNetErrorReload;

	private TextView new_title;//新闻标题
	private TextView new_date;//新闻时间
	private NetworkImageView new_image;//新闻图片
	private TextView new_content;//新闻内容

	private String id;//代表这个新闻的id
	private NewsListBean data;
	public static IUpdateServiceNewInfo iUpdateServiceNewInfo;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initView();
		AppManager.getAppManager().addActivity(this);
		initData();
		setListener();
	}

	public void setInterface(){
		iUpdateServiceNewInfo=new IUpdateServiceNewInfo() {

			@Override
			public void setServicNewInfo(NewsInfoBean newsInfoBean) {
				// TODO Auto-generated method stub
				if(!TextUtils.isEmpty(newsInfoBean.title))
				{
					new_title.setText(AppConstant.URLDecoderString(newsInfoBean.title));
				}if(!TextUtils.isEmpty(newsInfoBean.addtime))
				{
					new_date.setText(TimeUtils.parseLongToDate(newsInfoBean.addtime, "yyyy-MM-dd HH:mm"));
				}if(!TextUtils.isEmpty(newsInfoBean.content))
				{
					new_content.setText(AppConstant.URLDecoderString(newsInfoBean.content));
				}
				if(!TextUtils.isEmpty(newsInfoBean.imgurl))
				{
					
					new_image.setImageUrl(AppConstant.getHttpUrl(AppConstant.URLDecoderString(newsInfoBean.imgurl)),  AppContext.imageLoader);
				}
			}
		};
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.car_title_detail));
		commonTitleBar.setReturnVisible();
		if(AppContext.instance.isNetworkConnected()){
			//网络可用加载数据
			llNetError.setVisibility(View.GONE);
		} else {
			//网络不可用显示网络错误页面
			llNetError.setVisibility(View.VISIBLE);
		}
		setInterface();
		Intent intent=getIntent();
		id=intent.getStringExtra("id");
//		data=(NewsListBean) intent.getSerializableExtra("data");
		if(!TextUtils.isEmpty(id))
		{
			new GetNewsInfoAsyncTask(mAct, id).execute("");
		}
/*		if(data!=null)
		{
			if(!TextUtils.isEmpty(data.title))
			{
				new_title.setText(AppConstant.URLDecoderString(data.title));
			}if(!TextUtils.isEmpty(data.addtime))
			{
				new_date.setText(TimeUtils.parseLongToDate(data.addtime, "yyyy-MM-dd HH:mm"));
			}if(!TextUtils.isEmpty(data.description))
			{
				new_content.setText(AppConstant.URLDecoderString(data.description));
			}
			if(!TextUtils.isEmpty(data.imgurl))
				{
					new_image.setImageUrl(AppConstant.getHttpUrl(AppConstant.URLDecoderString(data.imgurl)),  AppContext.imageLoader);
				}
		}*/
		new_image.setDefaultImageResId(R.drawable.ic_launcher);
		new_image.setErrorImageResId(R.drawable.ic_launcher);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_service_news_details);
		mAct = this;
		commonTitleBar = (CommonTitleBar) findViewById(R.id.common_titlebar);
		llNetError = (LinearLayout)findViewById(R.id.llNetError);
		ivNetErrorReload = (ImageView)findViewById(R.id.ivNetErrorReload);
		new_title=(TextView)findViewById(R.id.new_title);
		new_date=(TextView)findViewById(R.id.new_date);
		new_image=(NetworkImageView)findViewById(R.id.new_image);
		new_content=(TextView)findViewById(R.id.new_content);

	}
	@Override
	protected void setListener() {
		commonTitleBar.setToolBarReturnOnclickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		ivNetErrorReload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(AppContext.instance.isNetworkConnected()){
					//网络可用加载数据
					llNetError.setVisibility(View.GONE);
					//TODO 做一些异步啊，或者数据的显示操作。
				} else {
					//网络不可用显示网络错误页面
					llNetError.setVisibility(View.VISIBLE);
				}
			}
		});

	}
	public interface IUpdateServiceNewInfo{
		public void setServicNewInfo(NewsInfoBean newsInfoBean);
	}
}
